import _ from 'lodash'

const _fas_mutual_info_ = {}

export function regist(relationID,relationEvt,objID,objEvt,vueInstance){

}
function _getGuid() {
    function t() {
        return ((65536 * (1 + Math.random())) | 0).toString(16).substring(1);
    }
    return (
        t() + t() + "-" + t() + "-" + t() + "-" + t() + "-" + t() + t() + t()
    );
  }
// relationObject:null,
// relationObjectEvent:null,
// responseEvent:null,
export function createSubscribeInfo(key,eventName,eventObjectList,element){
    let mainInfo = {
        key:key,
        eventName:eventName,
        eventObjectList:eventObjectList
    }
    let info = {
        "ID": _getGuid(),
        "Code": _getGuid(),
        "Name": "Sample Name",
        "Description": "Sample Description",
        "PortalInstanceID": element.options.target.pageID,
        "SourcePortletInstanceID": _getGuid(),
        "SourcePortletInstanceEventID": _getGuid(),
        //"SubscribeOrder": 85,
        "SubscribePortletInstanceID": _getGuid(),
        "SubscribeActionDescription": "",
        "SubscribeActionContent": JSON.stringify(mainInfo),
        // "Creator": null,
        // "CreateTime": "0001-01-01T00:00:00",
        // "LastModifier": null,
        // "LastModifyTime": "0001-01-01T00:00:00"
    }
    return info
}


export function createEvent(key,eventName,eventObjectList,element){
    debugger

    Widget[key].widget.Events = _.reject(Widget[key].widget.Events, function(evt) {
        return evt.Name == eventName && !evt.JSScriptES5
     });
     
    let calls = []
    eventObjectList.forEach(item=>{
        let script = ";Widget.$1.$2();\r\n".replace('$1',element.InstanceCode).replace('$2',item.responseEvent)
        calls.push(script)
    })
    
    const index = Widget[key].widget.Events.map(item => item.Name).indexOf(eventName);
    if(index != -1){
        Widget[key].widget.Events[index].ActionType = 0
        if(Widget[key].widget.Events[index].JSScriptES5){
            Widget[key].widget.Events[index].JSScriptES5 += calls.join('')
            Widget[key].widget.Events[index].JSScript = Widget[key].widget.Events[index].JSScriptES5
        }else 
        Widget[key].widget.Events[index].JSScript = Widget[key].widget.Events[index].JSScriptES5 = calls.join('')
        return 
    }

    let event = {
        "ID": _getGuid(),
        "Code": "",
        "Name": eventName,
        "ComponentID": null,
        "InstanceCode": key,
        "ActionType": 0,
        "ActionID": "",
        "DisplayInfo": "",
        "ActionSourceType": 0,
        "ActionSourceID": "",
        "JSScript": calls.join(''),
        "JSScriptES5": calls.join('')
    }
    Widget[key].widget.Events.push(event)
    //return event

}
export function createWidgetEvent(thizz){
    debugger
    let evtObject = _.chain(thizz.relationEventList).groupBy("relationObject").value()
    for(let comkey in evtObject){
        let infoList = evtObject[comkey]
        if(infoList == 1){
            // let evtName = infoList[0].relationObjectEvent
            // Name
            // let testRet = Widget[comkey].widget.Events.filter(item=>item.Name==evtName)
            // if(testRet.length > 0){

            // }
            // Widget[comkey].widget.Events.push(createEvent(comkey,evtName,infoList,thizz.element))
            createEvent(comkey,infoList[0].relationObjectEvent,infoList,thizz.element)
            thizz.element.options.relation.push(createSubscribeInfo(comkey,infoList[0].relationObjectEvent,infoList,thizz.element))
            continue
        }
        
        //多条按事件分类继续合并
       let subinfoList =  _.chain(infoList).groupBy("relationObjectEvent").value()
       for(let subcomkey in subinfoList){
            //Widget[comkey].widget.Events.push(createEvent(comkey,subcomkey,subinfoList[subcomkey],thizz.element))
            createEvent(comkey,subcomkey,subinfoList[subcomkey],thizz.element)
            thizz.element.options.relation.push(createSubscribeInfo(comkey,subcomkey,subinfoList[subcomkey],thizz.element))
       }
    }
    
}

export function toDBData(){

}

export function toWebData(){
    
}